package sv.mecca.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import sv.mecca.form.UsuariosForm;
import sv.mecca.modelo.UsuariosModelo;

public class TiposUsuariosAction extends DispatchAction {
	UsuariosModelo oper = new UsuariosModelo();

	/**
	 * Metodo para insertar un nuevo usuario
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void insertar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		oper.saveUsuario((UsuariosForm) form);
		this.listado(mapping, form, request, response);
	}

	/**
	 * Metodo para actualizar un registro de usuario
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void actualizar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		oper.updateUsuario((UsuariosForm) form);
		this.listado(mapping, form, request, response);
	}

	/**
	 * Metodo para eliminar un regisro de usuario
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void eliminar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		oper.deleteUsuario((UsuariosForm) form);
		this.listado(mapping, form, request, response);
	}

	/**
	 * Metodo para mostrar todos los usuarios registrados
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void listado(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("usuariosList", oper.listado());
		((UsuariosForm) form).destroy();
		request.getRequestDispatcher("/Definiciones/Usuarios.jsp").forward(
				request, response);
	}
}